package com.example.thirdstage_project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.thirdstage_project.common.Code;
import com.example.thirdstage_project.common.Result;
import com.example.thirdstage_project.entity.Expert;
import com.example.thirdstage_project.entity.User;
import com.example.thirdstage_project.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 86158
 * @version 1.0
 * Create by 2022/11/25 11:12
 */

/**
 * 专家信息管理 lei
 */
@RestController
@RequestMapping("/experts")
public class ExpertController {
    @Autowired
    private ExpertService expertService;
    /**
     * 分页
     * @return
     */
    @GetMapping("/all")
    public Result getPage2(int currentPage, int pageSize){
        IPage<User> page = expertService.getPage(currentPage, pageSize);
        if (currentPage > page.getPages()){
            page = expertService.getPage((int)page.getPages(), pageSize);
        }
        return new Result(Code.Page_OK,page);
    }
    /**
     * 查询
     *
     *
     */
//    @GetMapping
    public Result list(){
        List<Expert> list = expertService.selectAll();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败";
        return new Result(code,list,msg);
    }

    @DeleteMapping("/{expertid}")
    public Result delete(@PathVariable Integer expertid){
        boolean save = expertService.delete(expertid);
        return new Result(save ? Code.DELETE_OK: Code.DELETE_ERR ,save);
    }

    @PostMapping("/update")
    public Result update(Expert expert){
        boolean save = expertService.update(expert);
        return new Result(save ? Code.UPDATE_OK : Code.UPDATE_ERR,save);
    }
    @GetMapping
    public Result listId(int uid){
        Integer id = expertService.selectid(uid);
        Integer code = id != null ? Code.GET_OK : Code.GET_ERR;
        String msg = id != null ? "" : "数据查询失败";
        return new Result(code,id,msg);
    }
    /**
     * 根据密码查uid
     */
    @GetMapping("/select")
    public Result listuid(int uid){
        Integer id = expertService.selectuid(uid);
        Integer code = id != null ? Code.GET_OK : Code.GET_ERR;
        String msg = id != null ? "" : "数据查询失败";
        return new Result(code,id,msg);
    }

    /**
     * 修改密码
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(String password ,int uid ){
        boolean id = expertService.updatePassword(password ,uid);
        return new Result(id ? Code.UPDATE_OK : Code.UPDATE_ERR,id);
    }
}
